package com.jike.mahjong.common.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jike.mahjong.common.util.GsonUtil;

/**
 * 全局控制器
 * @author tearslee
 *
 */
@Controller
@RequestMapping(value="base")
public class BaseController {

	@Resource
    protected HttpServletRequest request;

    @Resource
    protected HttpServletResponse response;

    @Resource
    protected HttpSession session;
	
	protected BaseResult returnResult=null;
	
	protected Date nowDate=new Date();
	
	protected static Log log= LogFactory.getLog(BaseController.class);
	
	@SuppressWarnings("rawtypes")
	@ModelAttribute("returnResult")
	public String setReturnResult(HttpServletRequest request,@RequestParam Map<String,Object> map) throws UnsupportedEncodingException{
		if(map!=null && !map.keySet().isEmpty()){
			for (String obj : map.keySet()) {
				System.err.println(obj);
			}
		}
		request.setCharacterEncoding("UTF-8");
		returnResult=new BaseResult();
		return null;
	}
	
    @ExceptionHandler
    @ResponseBody
    public String exception(HttpServletRequest request, Exception e) {  
    	returnResult=new BaseResult();
    	if(e instanceof BadSqlGrammarException){    		
    		log.error("SQL错误："+((BadSqlGrammarException)e).getSql()+
    				"\n Cause:"+((BadSqlGrammarException)e).getCause());
    	}
    	returnResult.setError(true);
    	returnResult.setMsg(e.getMessage());
		return resultVal();
    } 
    /**
     * 404NotFound
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/difined")
    public String difined(HttpServletRequest request,HttpServletResponse response){
    	System.err.println("difined");
    	writeJson(response);
    	return null;
    }
    private void writeJson(HttpServletResponse response){
    	 PrintWriter out = null;
         try {
             response.setCharacterEncoding("UTF-8");
             response.setContentType("application/json; charset=utf-8");
             out = response.getWriter();
             out.write(resultVal()==null?"{}":resultVal());
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             if (out != null) {
                 out.close();
             }
         }
    }
    
	public BaseResult getReturnResult() {
		return returnResult;
	}

	public void setReturnResult(BaseResult returnResult) {
		this.returnResult = returnResult;
	}

	
	public String resultVal(){
		return GsonUtil.toJSONString(this.returnResult);
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletResponse getResponse() {
		return response;
	}

	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public HttpSession getSession() {
		return session;
	}
	/**
	 * 获取当前路径
	 * @return
	 */
	protected String basePath(){
		return request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
	}
}